home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 34 (1993-06)(MegaDisc Digital Publishing)(AU)(Disk 1 of 2)[WB].zip / MegaDisc 34 (1993-06)(MegaDisc Digital Publishing)(AU)(Disk 1 of 2)[WB].adf / Programs / IntConv32 / atoi32.asm next >
Assembly Source File  |  1993-06-21  |  1KB  |  76 lines

  1. ; (C) Peter Thompson 1993
  2. ; atol() and atoul() style routines for 32 bit integers.
  3.  
  4.     INCLUDE    "intdefs.i"
  5.     INCLUDE    "to32lib.i"
  6.  
  7.     IFD    ASM_ENTRY
  8.     INCLUDE    "intconv32.i"
  9.     ENDIF
  10.  
  11.     IFD    STKARGS
  12.     PUBLIC    _atou32
  13. _atou32:            ;C stkargs entry
  14.     move.l    4(SP),a0    ;C char *
  15.     ENDIF
  16.  
  17.     IFD    REGARGS
  18.     PUBLIC    @atou32
  19. @atou32:            ;C regargs entry
  20.     ENDIF
  21. a2u32:    lea    Tou32(PC),a1
  22.     bra    to32
  23.  
  24.     IFD    STKARGS
  25.     PUBLIC    _atoi32
  26. _atoi32:            ;C stkargs entry
  27.     move.l    4(SP),a0    ;C char *
  28.     ENDIF
  29.  
  30.     IFD    REGARGS
  31.     PUBLIC    @atoi32
  32. @atoi32:            ;C regargs entry
  33.     ENDIF
  34.  
  35. a2i32:    lea    Toi32(PC),a1
  36. to32:    push    Digit/Errno/a6
  37.     lea    au32(PC),a6
  38.     jsr    0(a1)
  39.     move.l    Errno,d1
  40.     pop    Digit/Errno/a6
  41.     rts
  42.  
  43.     ; convert base 10 ASCII string into unsigned 32-bit integer.
  44.  
  45. au32:    sub.b    #'0',Digit    ; get first digit
  46.     bcs    Derr32        ;V unless not valid digit
  47.     move.l    Digit,d0    ; and initialise number to it
  48.     cmp.b    #10,Digit    ;V
  49.     bcc    Derr32        ;V unless not valid digit
  50. au32a:
  51.     GetCh    Digit
  52.     sub.b    #'0',Digit
  53.     bcs    au32x        ; Less than '0',
  54.     cmp.b    #10,Digit
  55.     bcc    au32x        ; or more than '9', so exit.
  56. au32b:                ; multiply by 10 and overflow check.
  57.     add.l    d0,d0        ; *2
  58.     bcs    Verr32        ;V
  59.     move.l    d0,d1
  60.     add.l    d1,d1        ; *4
  61.     bcs    Verr32        ;V
  62.     add.l    d1,d1        ; *8
  63.     bcs    Verr32        ;V
  64.     add.l    d1,d0        ; *2 + *8 = *10.
  65.     bcs    Verr32        ;V
  66.     add.l    Digit,d0    ; + Digit
  67.     bcc    au32a        ;V SPECIAL - falls through if overflow.
  68. Verr32:    move.l    #ERR_RANGE,Errno    ; overflow!
  69. au32x:    UnGetCh
  70.     rts
  71. Derr32:    move.l    #ERR_NONUM,Errno    ; no digits in number!
  72.     bra    au32x
  73.  
  74.     END
  75.  
  76.